
package com.util;
import java.io.File;
import java.io.IOException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class Office2PDF {
	public static int office2PDF(String sourceFile, String destFile) {  
        Process pro = null;  
        OpenOfficeConnection connection = null;  
        try {  
            File inputFile = new File(sourceFile);  
            if (!inputFile.exists()) {  
                return -1;//文件不存在  
            }  
            //文件夹不存在创建目录  
            File outputFile = new File(destFile); 
            if(outputFile.exists()){
            	return 1;
            }
            if (!outputFile.getParentFile().exists()) {  
                outputFile.getParentFile().mkdirs();  
            }  
            String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4";  
            if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {  
                OpenOffice_HOME += "\\";  
            }  
            // 启动OpenOffice的服务    
            String command = OpenOffice_HOME  
                    + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";  
            pro = Runtime.getRuntime().exec(command);  
            // connect to an OpenOffice.org instance running on port 8100  
            connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);  
            connection.connect();  
            // convert  
            DocumentConverter converter = new OpenOfficeDocumentConverter(  
                    connection);  
            converter.convert(inputFile, outputFile);
            return 0;  
        } catch (Exception e) {  
            e.printStackTrace();  
            
        }finally{  
            if(connection!=null){  
                // close the connection  
                connection.disconnect();  
            }  
             // 关闭OpenOffice服务的进程    
            if(pro!=null){  
                pro.destroy();  
            }  
        }  
        return 1;  
    }
	public static Process pro = null;  
    public static OpenOfficeConnection connection = null;
    
	public  static DocumentConverter startService(){
		
        String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4";  
        if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {  
            OpenOffice_HOME += "\\";  
        }  
        // 启动OpenOffice的服务    
        String command = OpenOffice_HOME  
                + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";  
        try {
			pro = Runtime.getRuntime().exec(command);
			 connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);  
		        connection.connect();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        // connect to an OpenOffice.org instance running on port 8100  
        DocumentConverter converter = new OpenOfficeDocumentConverter(  
                connection);
        return converter;
	}
	
	public static void stopService(){
        connection.disconnect();  
        pro.destroy();  
	}
}